﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Discovery;
using XrmLibrary.Adapters;
using XrmLibrary.ConfigFile;
using XrmLibrary.Configurations;
using XrmLibrary.Exceptions;
using DiscoveryServiceAdapter = XrmLibrary.MsCrm4Adapter.Adapters.DiscoveryServiceAdapter;
using OrganizationServiceAdapter = XrmLibrary.MsCrm4Adapter.Adapters.OrganizationServiceAdapter;

namespace XrmLibrary.MsCrm4Adapter.Configurations
{
    public class CrmConfiguration : CrmConfigurationBase
    {
        private const String CrmServerUrlName = "crmServerUrl";
        private const String CrmDiscoveryUrlName = "crmDiscoveryUrl";
        private const String CrmOrganizationNameName = "crmOrganizationName";
        private const String CrmAuthenticationTypeName = "crmAuthenticationType";
        private const String CrmUserNameName = "crmUserName";
        private const String CrmPasswordName = "crmPassword";

        private OrganizationServiceAdapter _organizationServiceAdapter;
        private DiscoveryServiceAdapter _discoveryServiceAdapter;

        public override void Load()
        {
            String crmServerUrl = AdapterConfiguration.GetDynamicValue(CrmServerUrlName);
            String crmOrganizationName = AdapterConfiguration.GetDynamicValue(CrmOrganizationNameName);
            _organizationServiceAdapter = OrganizationServiceAdapter.Instantiate(crmServerUrl, crmOrganizationName);
            //_discoveryServiceAdapter = DiscoveryServiceAdapter.Instantiate(crmServerUrl, crmOrganizationName);
        }

        public override String GetConfigurationName()
        {
            return AdapterConfiguration.GetConfigurationName();
        }

        public override bool IsCrmDiscoveryServiceConfigured
        {
            get { return false; }
            protected set { throw new NotImplementedException(); }
        }

        public override IOrganizationServiceAdapter CreateOrganizationServiceAdapter()
        {
            return _organizationServiceAdapter;
        }

        public override IDiscoveryServiceAdapter CreateDiscoveryServiceAdapter()
        {
            return _discoveryServiceAdapter;
        }

        public override void Reinitialize(ClientCredentials credentials)
        {
            throw new NotImplementedException();
        }
    }
}
